#!/usr/bin/env python

from system_class import System
import os, sys

def init ( l, admin = 0 ):
	from liwe import parts

	res = []

	parts.add ( l.layman, [ "base", "form" ] )

	l.layman.add_js ( "modules/system/js/system.js" )
	l.layman.add_css ( "modules/system/gfx/system.css" )

	if ( admin and int ( admin, 10 ) == 1 and l.cfg.get ( "bootstrap.admin", 0 ) ) or ( admin == 0 and l.cfg.get ( "bootstrap.site", 0 ) ):
		if l.cfg.get ( "debug.enabled", 0 ):
			l.layman.add_css ( "modules/system/gfx/bootstrap/css/bootstrap.min.css" )
		else:
			l.layman.add_css ( "modules/system/gfx/bootstrap/css/bootstrap.css" )
		


	l.layman.add_js_code ( "system.init (); system.plugins_autoreplace ();" )

	if admin and int ( admin, 10 ) == 1:
		s = System ( l )

		parts.add ( l.layman, [ "ext" ] )
		parts.add_wwl ( l.layman, [ "tabs", "accordion", "calendar", "grid" ] )

		l.layman.add_js ( "modules/system/js/system.admin.js" )
		l.layman.add_css ( "modules/system/gfx/system.admin.css" )

		if l.cfg.get ( "bootstrap.admin", 0 ):
                	l.layman.add_css ( "modules/system/gfx/theme_base.css" )
                	l.layman.add_css ( "modules/system/gfx/font_awesome.css" )

		l.layman.add_js_code ( "system.admin.init ();", tail = 1 )
		l.layman.add_body_class ( "system" )

		if l.pyhp.session.data.get ( 'uid', 0 ) != 0:
			res.append ( s.templates [ 'system-admin' ] )
		else:
			title = l.cfg.get ( 'site.name', '' )
			res.append ( s.templates [ 'system-login' ] % { "title" : title } )

	return ''.join ( res )


def main_content ( liwe, default = "" ):
	"""
	:INPUT:
		- id_news:              SAFE_NUMBER
	"""
	uri_data = liwe.uri_data

	mod = uri_data.get ( "module" )
	dct = {}

	if mod:
		action = uri_data.get ( "action" )
		dct = uri_data
		res = liwe.invoke ( "%s.%s" % ( mod, action ), ** dct )
	else:
		if default:
			res = []
			for act in default.split ( "|" ):
				x = liwe.invoke ( act, ** dct ) 

				if "_txt" in x: res.append ( x [ "_txt" ] )

			res = ''.join ( res )
		else:
			res = "MAIN ACTION SHOULD BE DEFINED IN system.main_content call"

	return res

def autoload ( liwe, admin = 0 ):
	"""
	"""
	s = System ( liwe )
	lm = liwe.layman

	res = []

	res.append ( lm.execute ( ( "system", "init" ), { "admin" : admin } ) )

	if admin and liwe.pyhp.session.data.get ( 'uid', 0 ) == 0:
		mods = [ 'user' ]
	else:
		mods = s.list_modules ()
		mods.sort ()

	for mod in mods:
		if mod in ( "system", "site"): continue

		res.append ( lm.execute ( ( mod, "init" ), { "admin" : admin } ) )

	if not admin: res.append ( lm.execute ( ( "site", "init" ), { "admin" : admin } ) )

	return ''.join ( res )
		
	#liwe.layman.execute 

def url_redir ( liwe ):
	uri_data = liwe.uri_data

	mod = uri_data.get ( "module" )
	dct = {}

	if mod:
		obj = None
		if mod == 'news':
			from modules.news.news_class import News
			obj = News ( liwe )
			uri_data [ "page" ] = "news_show"
			uri_data [ "dest" ] = "full_news"

		elif mod == 'staticpage':
			from modules.staticpage.staticpage_class import StaticPage
			obj = StaticPage ( liwe )
			# non esiste la funzione "generica" ma solo nel site (vedi sito OS3)
			#uri_data [ "page" ] = "show_page"

		if obj:
			liwe.layman.redirect ( "/" + obj.url_to_history ( uri_data ) )

	return ' '

def build_sitemaps ( liwe ):
	import datetime

	now = datetime.datetime.now ().isoformat ( "T" ).split ( "." ) [ 0 ]

	s = System ( liwe )


	data = liwe.invoke ( "system.ajax.modules_sitemap" )

	base_path = os.path.join ( "site", "sitemaps" )

	maps = []

	for mod in data [ 'sitemaps' ]:
		modname = mod [ 'module' ]

		sitemap = mod [ 'data' ] [ 'sitemap' ]
		if len ( sitemap ) == 0: continue
		
		maps.append ( modname )

		fname = os.path.join ( base_path, modname + ".xml" )
		outf  = open ( fname, "w" )

		fil = []

		fil.append ( s.templates [ 'sitemap-header' ] )

		for d in sitemap:
			fil.append ( s.templates [ 'sitemap-url' ] % d )

		fil.append ( s.templates [ 'sitemap-footer' ] )

		res = ''.join ( fil ).encode ( 'ascii', 'xmlcharrefreplace' )
		outf.write ( res )
		outf.close ()

	outf = open ( "sitemap.xml", "w" )

	fil = []

	fil.append ( s.templates [ 'sitemap-index-header' ] )
	
	for name in maps:
		dct = { "loc" : "%s/site/sitemaps/%s.xml" % ( liwe.base_url, name ),
			"lastmod" : now }

		fil.append ( s.templates [ 'sitemap-index-row' ] % dct  )
		

	fil.append ( s.templates [ 'sitemap-index-footer' ] )
	res = ''.join ( fil ).encode ( 'ascii', 'xmlcharrefreplace' )
	outf.write ( res )

	return 'BUILD SITEMAPS DONE'
